<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
    
     <div class="container-div">
		<div class="row">
			<div class="col-sm-12 search-collapse">
				<form id="formId">
					<div class="select-list">
						<ul>
							<li>
								节点名称：<input type="text" name="nodeName"/>
							</li>

							<li>
								ip地址：<input type="text" name="ip"/>
							</li>


							<li>
								节点机用户名：<input type="text" name="userName"/>
							</li>


							<li>
								客户端状态： <select id="status" name="status">
								<option value="" >全部</option>
								<option value="1" >状态正常</option>
								<option value="0" >状态异常</option>
								<option value="-1" >Jmeter已关闭</option>
							</select>
							</li>

							<li>
								系统类型： <select id="os" name="os">
								<option value="" >全部</option>
								<option value="windows" >windows</option>
								<option value="linux" >linux</option>
							</select>
							</li>

							<li>
								<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
								<a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
							</li>
						</ul>
					</div>
				</form>
			</div>
			
	        <div class="btn-group-sm hidden-xs" id="toolbar" role="group">

				<a class="btn btn-primary btn-edit disabled" onclick="$.operate.edit()" shiro:hasPermission="jmeter:jmeterNode:edit">
					<i class="fa fa-edit"></i> 修改
				</a>
				<a class="btn btn-danger btn-del btn-del disabled" onclick="$.operate.removeAll()" shiro:hasPermission="jmeter:jmeterNode:remove">
					<i class="fa fa-remove"></i> 删除
				</a>
				<a class="btn btn-primary btn-del btn-del disabled" onclick="startJmeter()" shiro:hasPermission="jmeter:jmeterNode:start">
					<i class="fa fa-play"></i> 启动Jmeter
				</a>

				<a class="btn btn-danger btn-del btn-del disabled" onclick="stopJmeter()" >
					<i class="fa fa-stop"></i> 关闭Jmeter
				</a>
				<a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="jmeter:jmeterNode:export">
						<i class="fa fa-download"></i> 导出
				 </a>
			</div>
			<div class="col-sm-12 select-table table-striped">
				<table id="bootstrap-table" data-mobile-responsive="true"></table>
			</div>
		</div>
	</div>
    <div th:include="include :: footer"></div>
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('jmeter:jmeterNode:edit')}]];
        var removeFlag = [[${@permission.hasPermi('jmeter:jmeterNode:remove')}]];
        var prefix = ctx + "jmeter/jmeterNode";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
				exportUrl: prefix + "/export",
                modalName: "性能测试分布式节点",
				onLoadSuccess: function (data) {
						return console.log(data);
				},
				uniqueId:"id",
				search: false,
		        showExport: true,
                columns: [{
		            checkbox: true
		        },
				{
					field : 'id',
					title : '',
					visible: false
				},
				{
					field : 'nodeName',
					title : 'Jmeter节点名称',
					sortable: true
				},
				{
					field: 'status',
					title: '客户端状态',
					width: '10%',
					class: 'myTDLengthHidden',
					formatter: function (value,
										 row, index) {
						if (value == 1) {
							var hdiv = '<span id="statusSpan' + row.id + '" class="glyphicon glyphicon-ok-sign" style="color: rgb(0, 175, 0);"> 状态正常</span>';
						} else if (value == 0) {
							var hdiv = '<span id="statusSpan' + row.id + '" class="glyphicon glyphicon-minus-sign" style="color: rgb(199, 0, 0);"> 状态异常</span>';
						} else if (value == -1) {
							var hdiv = '<span id="statusSpan' + row.id + '" class="glyphicon glyphicon-question-sign" style="color: rgba(1,6,14,0.96);"> Jmeter已关闭</span>';
						} else {
							var hdiv = '<span id="statusSpan' + row.id + '" class="glyphicon glyphicon-question-sign" style="color: rgb(255, 168, 0); font-size: 14px;"> 状态未知</span>';
						}
						return '<font id="statusFont' + row.id + '">' + hdiv + '</font>';
					}
				},
				// {
				// 	field : 'nodePort',
				// 	title : '节点端口',
				// 	sortable: true
				// },
				{
					field : 'ip',
					title : 'ip地址',
					sortable: true
				},
				{
					field : 'jmeterPort',
					title : 'Jmeter链接端口号',
					sortable: true
				},
				{
					field : 'userName',
					title : '节点机用户名',
					sortable: true
				},
				{
					field : 'passwd',
					title : '节点机密码',
					sortable: true
				},
				{
					field : 'maxConcurrency',
					title : '最大并发数 ',
					formatter:function(v,r,i){
						return v==-1?'-':v;
					},
					sortable: true
				},
				{
					field : 'cpu',
					title : 'cpu ',
					formatter:function(v,r,i){
						return v==-1?'-':v;
					},
					sortable: true
				},
				{
					field : 'memory',
					title : '内存 单位g ',
					formatter:function(v,r,i){
						return v==-1?'-':v;
					},
					sortable: true
				},
				{
					field : 'disk',
					title : '磁盘 单位g  ',
					formatter:function(v,r,i){
						return v==-1?'-':v;
					},
					sortable: true
				},
				{
					field : 'io',
					title : 'io ',
					formatter:function(v,r,i){
						return v==-1?'-':v;
					},
					sortable: true
				},
				{
					field : 'os',
					title : '操作系统',
					sortable: true
				},
				{
					field : 'homeDir',
					title : '子节点的Jmeter路径',
					sortable: true
				},
				{
					field : 'addTime',
					title : '创建时间',
					formatter: function (value,
										 row, index) {
						return $.common.formatTime(value);
					}
				},
				{
					field : 'addBy',
					title : '提交用户id',
					sortable: true
				},
				{
					field : 'updateTime',
					title : '修改时间',
					formatter: function (value,
										 row, index) {
						return $.common.formatTime(value);
					}
				},
				{
					field : 'updateBy',
					title : '修改用户id',
					sortable: true
				},
		        {
		            title: '操作',
		            align: 'center',
		            formatter: function(value, row, index) {
		            	var actions = [];
		            	actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="#" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
						return actions.join('');
		            }
		        }


		        ]
            };
            $.table.init(options);
        });


		/*刷新客户端状态*/
		function refreshClientStatus(clientId) {
			var url = prefix + "/getNodeStatus?id=" + clientId;
			$.getJSON(url, null, function call(result) {

				if(result.type=='timeout'){
					setTimeout(function () {
						refreshClientStatus(clientId)
					})
				}

				//update table
				// $('#bootstrap-table').bootstrapTable('refresh');
				$('#bootstrap-table').bootstrapTable('updateByUniqueId', {
					Id: clientId,
					row: {
						status: result.status
					}
				});


				if (result.status == 1) {
					/*删除老的span*/
					var oldSpan = document.getElementById("statusSpan" + clientId);
					oldSpan.parentNode.removeChild(oldSpan);
					/*新建一个待更新span*/
					var updateSpan = document.createElement("span");
					updateSpan.innerText = "状态正常"
					updateSpan.setAttribute("class", "glyphicon glyphicon-ok-sign");
					updateSpan.setAttribute("style", "color: rgb(0, 175, 0)");
					updateSpan.setAttribute("id", "statusSpan" + clientId);
					document.getElementById("statusFont" + clientId).appendChild(updateSpan);
				} else if (result.status == 0) {
					/*删除老的span*/
					var oldSpan = document.getElementById("statusSpan" + clientId);
					oldSpan.parentNode.removeChild(oldSpan);
					/*新建一个待更新span*/
					var updateSpan = document.createElement("span");
					updateSpan.innerText = "状态异常"
					updateSpan.setAttribute("class", "glyphicon glyphicon-minus-sign");
					updateSpan.setAttribute("style", "color: rgb(199, 0, 0);");
					updateSpan.setAttribute("id", "statusSpan" + clientId);
					document.getElementById("statusFont" + clientId).appendChild(updateSpan);
				} else if (result.status == -1) {
					/*删除老的span*/
					var oldSpan = document.getElementById("statusSpan" + clientId);
					oldSpan.parentNode.removeChild(oldSpan);
					/*新建一个待更新span*/
					var updateSpan = document.createElement("span");
					updateSpan.innerText = "Jmeter已关闭"
					updateSpan.setAttribute("class", "glyphicon glyphicon-question-sign");
					updateSpan.setAttribute("style", "color: rgba(1,6,14,0.96);");
					updateSpan.setAttribute("id", "statusSpan" + clientId);
					document.getElementById("statusFont" + clientId).appendChild(updateSpan);
				}
				//
				// setTimeout(function () {
				// 	refreshClientStatus(clientId)
				// })
			});
		}


		// $(function () {
		// 	$.map($('#bootstrap-table').bootstrapTable('getData'), function (row) {
		// 		refreshClientStatus(clientId)
		// 	});
		//
		//
		// });


		// $('#bootstrap-table').on('load-success.bs.table', function (e,data) {
		// 	data.rows.map(it=>
		// 			setTimeout(function () {
		// 				refreshClientStatus(it.id)
		// 			})
		// 	);
		// });


		function startJmeter() {
			let arr=$('#bootstrap-table').bootstrapTable('getSelections');
			if(arr.length==0){
				$.modal.alertWarning("请至少选择一条记录");
				return;
			}

			let disabled=arr.filter(it=>it.status==-1);
			if(disabled.length==0){
				$.modal.alertWarning("没有需要启动的客户端");
				return;
			}

			var idlist=disabled.map(it=>it.id);

			var config = {
				url: prefix + "/startJmeters",
				type: "post",
				dataType: "json",
				contentType:"application/json",
				data: JSON.stringify(idlist),
				beforeSend: function () {
				},
				success: function(result) {
					$('#bootstrap-table').bootstrapTable('refresh')
				}
			};
			$.ajax(config);
		}


		function stopJmeter() {
			let arr=$('#bootstrap-table').bootstrapTable('getSelections');
			if(arr.length==0){
				$.modal.alertWarning("请至少选择一条记录");
				return;
			}

			let disabled=arr.filter(it=>it.status==1);
			if(disabled.length==0){
				$.modal.alertWarning("没有需要关闭的客户端");
				return;
			}

			var idlist=disabled.map(it=>it.id);

			var config = {
				url: prefix + "/stopJmeter",
				type: "post",
				dataType: "json",
				contentType:"application/json",
				data: JSON.stringify(idlist),
				beforeSend: function () {
				},
				success: function(result) {
					$('#bootstrap-table').bootstrapTable('refresh')
				}
			};
			$.ajax(config);
		}

    </script>
</body>
</html>